define('apps/common/TopSearch', ['core/jQuery', 'core/ajax/Ajax', 'core/Toggle', 'apps/common/Suggestion', 'core/List'], function(require) {

	var $ = require('core/jQuery'),
		Toggle = require('core/Toggle'),
		Ajax = require('core/ajax/Ajax'),
		Suggestion = require('apps/common/Suggestion'),
		List = require('core/List');

	function TopSearch(opts) {
		this.input = null;
		this.button = null;
		this.URL = null;
		this.suggestList = null;
		this.searchURL = null;

		K.mix(this, opts);

		this.init();
	}

	TopSearch.prototype = {
		'init' : function() {
			this.initList();
			this.suggestion = new Suggestion({'URL' : this.URL});
			this.bindEvents();
		},

		/**
         * 初始化suggest列表
         */
        'initList' : function() {
            var _this = this;
            this.list = new List({
                'trigger' : this.input,
                'container' : this.suggestList,
                'getValue' : function() {
                    var focusedItem = this.container.find(this.itemSelector + '.' + this.itemHoverClass);
                    return {
                        'name' : focusedItem.data('name'),
						'url' : focusedItem.data('url')
                    };
                },

                'updateList' : function(html) {
                    this.container.html(html);
                    this.container.find(this.itemSelector).first().addClass(this.itemHoverClass);
                    this.container.find('.inner_last').next(this.itemSelector).addClass('inner_first');
                }
            });

			this.list.on('outputvalue', $.proxy(this.setSuggest, this));
        },

		'bindEvents' : function() {
			this.input
				.bind('input propertychange focus', $.proxy(this.showSuggest, this))
				.bind('keydown', $.proxy(this.inputSearch, this));
			
			this.button.bind('click', $.proxy(this.buttonSearch, this));

			this.initToggle();
		},

        'buttonSearch' : function(ev) {
			var key = K.trim(this.input.val());
			if(key.length != 0) {
				Ajax.get(this.searchURL, {'kw':key},
					function(data){
						window.location.href = data.link;
					},
					function(error){
						K.log('Err Data:', data);
					}
				);
			} else {
				alert('请输入搜索内容');
			}

		},

		'inputSearch' : function(ev) {
			if(ev.keyCode === 13) {
				if(this.suggestting) {
					this.buttonSearch();
				}
			}
		},
						
		'showSuggest': function(ev) {
			var key = this.input.val(),
				target = $(ev.currentTarget);
				
			this.suggestting = true;
			target.closest('.form_topsearch').addClass('focus');
			
			if(key.length) {
				this.suggestion.getSuggestion({'kw' : key}, $.proxy(function(data) {
					this.suggestting = false;
					if(data.html && data.html.length) {
						this.list.show(data.html);
					} else {
						this.list.hide();
					}
				}, this));
			} else {
				this.list.hide();
			}
			ev.preventDefault();
            ev.stopPropagation();
        },

		'setSuggest' : function(args) {
			if(this.suggestting) return;
			
			var url = args.data.url;
            this.input.trigger('blur');
			$('.form_topsearch').removeClass('focus');
            this.suggestList.hide();
			if(url) {
				window.location.href = url;
			}
		},

		'initToggle': function() {
			var _this = this;
            new Toggle({ 'trigger': this.input, 'board': this.suggestList, 'handler': function(){
                    //_this.input.val('');
					$('.form_topsearch').removeClass('focus');
					_this.suggestList.hide();
            }});
        }
	};

	return TopSearch;

});